package com.fwj.authorization.staff.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fwj.authorization.staff.entity.SysPermissionRole;
import com.fwj.authorization.staff.entity.SysRoleButton;
import com.fwj.authorization.staff.entity.SysRoleMenu;
import com.fwj.authorization.staff.service.*;
import com.fwj.base.entity.R;
import com.fwj.base.exception.CustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 角色权限
 *
 * @author 樊温军
 * @date 2025/3/18 上午10:32
 */
@Slf4j
@RestController
@RequestMapping("/")
@Api(tags = "角色权限")
@RequiredArgsConstructor
public class SysAuthController {

    private final SysRoleService sysRoleService;
    private final SysUserRoleService sysUserRoleService;
    private final SysPermissionService sysPermissionService;
    private final SysPermissionRoleService sysPermissionRoleService;
    private final SysRoleMenuService sysRoleMenuService;
    private final SysRoleButtonService sysRoleButtonService;

    /**
     * 权限角色码—分页
     *
     * @param page              分页对象
     * @param sysPermissionRole 查询条件
     * @return R<IPage < SysPermissionRole>>
     */
    @ApiOperation(value = "权限角色码—分页")
    @GetMapping("/permissionRole/page")
    public R<IPage<SysPermissionRole>> getPage(Page<SysPermissionRole> page, SysPermissionRole sysPermissionRole) {
        try {
            log.info("权限角色码—分页，分页对象：{}，参数：{}", JSONUtil.toJsonStr(page), JSONUtil.toJsonStr(sysPermissionRole));
            IPage<SysPermissionRole> iPage = sysPermissionRoleService.getPage(page, sysPermissionRole);
            log.info("权限角色码—分页，返回结果：{}", JSONUtil.toJsonStr(iPage));
            return R.ok(iPage, "查询成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }


    /**
     * 权限角色码—保存
     *
     * @param sysPermissionRoles 权限角色码列表
     * @return R<Boolean>
     */
    @ApiOperation(value = "权限角色码—保存")
    @PostMapping("/permissionRole/save")
    public R<Boolean> savePermissionRole(@Valid @RequestBody List<SysPermissionRole> sysPermissionRoles) {
        try {
            log.info("权限角色码—保存，参数：{}", JSONUtil.toJsonStr(sysPermissionRoles));
            Boolean flag = sysPermissionRoleService.savePermissionRole(sysPermissionRoles);
            log.info("权限角色码—保存，返回结果：{}", flag);
            return R.ok(flag, "保存成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 权限角色码—批量删除
     *
     * @param ids 权限角色码ID列表
     * @return R<Boolean>
     */
    @ApiOperation(value = "权限角色码—批量删除")
    @PostMapping("/permissionRole/del")
    public R<Boolean> delPermissionRole(@Valid @RequestBody List<Long> ids) {
        try {
            log.info("权限角色码—批量删除，参数：{}", JSONUtil.toJsonStr(ids));
            Boolean flag = sysPermissionRoleService.removeByIds(ids);
            log.info("权限角色码—批量删除，返回结果：{}", flag);
            return R.ok(flag, "保存成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }


    /**
     * 菜单角色权限—分页
     *
     * @param page        分页对象
     * @param sysRoleMenu 查询条件
     * @return R<IPage < SysRoleMenu>>
     */
    @ApiOperation(value = "菜单角色权限—分页")
    @GetMapping("/roleMenu/page")
    public R<IPage<SysRoleMenu>> getPage(Page<SysRoleMenu> page, SysRoleMenu sysRoleMenu) {
        try {
            log.info("菜单角色权限—分页，分页对象：{}，参数：{}", JSONUtil.toJsonStr(page), JSONUtil.toJsonStr(sysRoleMenu));
            IPage<SysRoleMenu> iPage = sysRoleMenuService.getPage(page, sysRoleMenu);
            log.info("菜单角色权限—分页，返回结果：{}", JSONUtil.toJsonStr(iPage));
            return R.ok(iPage, "查询成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }


    /**
     * 菜单角色权限—保存
     *
     * @param sysRoleMenus 菜单角色权限列表
     * @return R<Boolean>
     */
    @ApiOperation(value = "菜单角色权限—保存")
    @PostMapping("/roleMenu/save")
    public R<Boolean> saveRoleMenu(@Valid @RequestBody List<SysRoleMenu> sysRoleMenus) {
        try {
            log.info("菜单角色权限—保存，参数：{}", JSONUtil.toJsonStr(sysRoleMenus));
            Boolean flag = sysRoleMenuService.saveRoleMenu(sysRoleMenus);
            log.info("菜单角色权限—保存，返回结果：{}", flag);
            return R.ok(flag, "保存成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 菜单角色权限—批量删除
     *
     * @param ids 菜单角色权限ID列表
     * @return R<Boolean>
     */
    @ApiOperation(value = "菜单角色权限—批量删除")
    @PostMapping("/roleMenu/del")
    public R<Boolean> delRoleMenu(@Valid @RequestBody List<Long> ids) {
        try {
            log.info("菜单角色权限—批量删除，参数：{}", JSONUtil.toJsonStr(ids));
            Boolean flag = sysRoleMenuService.removeByIds(ids);
            log.info("菜单角色权限—批量删除，返回结果：{}", flag);
            return R.ok(flag, "保存成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }


    /**
     * 按钮角色权限—分页
     *
     * @param page          分页对象
     * @param sysRoleButton 查询条件
     * @return R<IPage < SysRoleButton>>
     */
    @ApiOperation(value = "按钮角色权限—分页")
    @GetMapping("/roleButton/page")
    public R<IPage<SysRoleButton>> getPage(Page<SysRoleButton> page, SysRoleButton sysRoleButton) {
        try {
            log.info("按钮角色权限—分页，分页对象：{}，参数：{}", JSONUtil.toJsonStr(page), JSONUtil.toJsonStr(sysRoleButton));
            IPage<SysRoleButton> iPage = sysRoleButtonService.getPage(page, sysRoleButton);
            log.info("按钮角色权限—分页，返回结果：{}", JSONUtil.toJsonStr(iPage));
            return R.ok(iPage, "查询成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }


    /**
     * 按钮角色权限—保存
     *
     * @param sysRoleButtons 按钮角色权限列表
     * @return R<Boolean>
     */
    @ApiOperation(value = "按钮角色权限—保存")
    @PostMapping("/roleButton/save")
    public R<Boolean> saveRoleButton(@Valid @RequestBody List<SysRoleButton> sysRoleButtons) {
        try {
            log.info("按钮角色权限—保存，参数：{}", JSONUtil.toJsonStr(sysRoleButtons));
            Boolean flag = sysRoleButtonService.saveRoleButton(sysRoleButtons);
            log.info("按钮角色权限—保存，返回结果：{}", flag);
            return R.ok(flag, "保存成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 按钮角色权限—批量删除
     *
     * @param ids 按钮角色权限ID列表
     * @return R<Boolean>
     */
    @ApiOperation(value = "按钮角色权限—批量删除")
    @PostMapping("/roleButton/del")
    public R<Boolean> delRoleButton(@Valid @RequestBody List<Long> ids) {
        try {
            log.info("按钮角色权限—批量删除，参数：{}", JSONUtil.toJsonStr(ids));
            Boolean flag = sysRoleButtonService.removeByIds(ids);
            log.info("按钮角色权限—批量删除，返回结果：{}", flag);
            return R.ok(flag, "保存成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }
}

